package interview.juc;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TestSocket {
    private Lock lock = new ReentrantLock();

    public synchronized void lock(){
        System.out.println("lock "+Thread.currentThread().getName());
        try {
            Thread.sleep(6000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }


    public static void main(String[] args) throws InterruptedException {
        TestSocket testSocket = new TestSocket();
        new Thread(testSocket::lock,"A").start();
        new Thread(testSocket::lock,"B").start();
        new Thread(()->{
            BlockingQueue<String> bq = new ArrayBlockingQueue<>(3);
            try {
                bq.put("A");
                bq.put("B");
                bq.put("C");
                bq.put("D");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"C").start();
        Thread.sleep(5000);

        ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
        int noThreads = currentGroup.activeCount();
        Thread[] lstThreads = new Thread[noThreads];
        currentGroup.enumerate(lstThreads);

        for (int i = 0; i < noThreads; i++){
            System.out.println("Thread No:" + i + " = " + lstThreads[i].getName()+" state " +lstThreads[i].getState());
        }
    }

}
